<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        table {
            width: 300px;
            margin: 100px auto;
            border-collapse: collapse;
            text-align: center;
        }
        
        thead th {
            background-color: skyblue;
            height: 30px;
            font-size: 16px;
        }
        
        tbody td {
            height: 25px;
            font-size: 14px;
            border-bottom: 1px solid #d7d7d7;
        }
        
        tbody tr {
            background-color: #d7d7d7;
        }
        
        .bg {
            background-color: pink;
        }
    </style>
</head>

<body>
    <div class="wrap">
        <table>
            <thead>
                <tr>
                    <th>
                        <input type="checkbox" id="j_cbAll">
                    </th>
                    <th>商品</th>
                    <th>价格</th>
                </tr>
            </thead>
            <tbody id="j_tb">
                <tr>
                    <td>
                        <input type="checkbox">
                    </td>
                    <td>iPhone8</td>
                    <td>8000</td>
                </tr>
                <tr>
                    <td>
                        <input type="checkbox">
                    </td>
                    <td>iPhone9</td>
                    <td>9000</td>
                </tr>
                <tr>
                    <td>
                        <input type="checkbox">
                    </td>
                    <td>iPhoneX</td>
                    <td>10000</td>
                </tr>
                <tr>
                    <td>
                        <input type="checkbox">
                    </td>
                    <td>iPhone12</td>
                    <td>11000</td>
                </tr>
            </tbody>
        </table>
    </div>
    <script>
        // 鼠标移动 增加条目背景色
        var tds = document.querySelector('tbody').querySelectorAll('tr')
        for (var i = 0; i < tds.length; i++) {
            tds[i].onmouseover = function() {
                this.className = 'bg';
            }
            tds[i].onmouseout = function() {
                this.className = '';
            }
        }

        // 全选和反选
        // 全选input
        var j_cbAll = document.getElementById('j_cbAll');
        // 下面单选
        var j_tbs = document.getElementById('j_tb').getElementsByTagName('input');
        // 全选input事件
        j_cbAll.onclick = function() {
                // 设置所有子input的选中状态和全选input状态相同
                for (var i = 0; i < j_tbs.length; i++) {
                    j_tbs[i].checked = this.checked;
                }
            }
            // 子input事件
        for (var i = 0; i < j_tbs.length; i++) {
            j_tbs[i].onclick = function() {
                // 控制全选按钮是否选中
                var flag = true;
                for (var i = 0; i < j_tbs.length; i++) {
                    if (!j_tbs[i].checked) {
                        flag = false;
                    }
                }
                j_cbAll.checked = flag;
            }
        }
    </script>
</body>

</html>